﻿using System;
using System.Collections.Generic;
using System.Text;
using Common.Utilities.Dispatcher;
using Common.Utilities;
using System.Xml;

namespace Workflows.Components.Rules
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class CodeRuleOutput:ResultBase
    {
        #region props
        private string _AssemblyName;
        /// <summary></summary>
        public string AssemblyName
        {
            get
            {
                return this._AssemblyName;
            }
            set
            {
                this._AssemblyName = value;
            }
        }

        private string _OutputTypeName;
        /// <summary></summary>
        public string OutputTypeName
        {
            get
            {
                return this._OutputTypeName;
            }
            set
            {
                this._OutputTypeName = value;
            }
        }

        private string _SerializedOutputValue;
        /// <summary></summary>
        public string SerializedOutputValue
        {
            get
            {
                return this._SerializedOutputValue;
            }
            set
            {
                this._SerializedOutputValue = value;
            }
        }

        private bool _EvaluationResult;
        /// <summary></summary>
        public bool EvaluationResult
        {
            get
            {
                return this._EvaluationResult;
            }
            set
            {
                this._EvaluationResult = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public CodeRuleOutput()
            : base(Guid.Empty, Guid.Empty)
        {
            this._AssemblyName = string.Empty;
            this._OutputTypeName = typeof(string).FullName;
            this._SerializedOutputValue = string.Empty;
            this._EvaluationResult = false;
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="assemblyName"></param>
        /// <param name="outputTypeName"></param>
        /// <param name="outputSerializedValue"></param>
        /// <param name="evalResult"></param>
        public CodeRuleOutput(Guid componentID, Guid jobTicketID,
            string assemblyName, 
            string outputTypeName, 
            string outputSerializedValue,
            bool evalResult)
            : base(componentID, jobTicketID)
        {
            this._AssemblyName = assemblyName;
            this._OutputTypeName = outputTypeName;
            this._SerializedOutputValue = outputSerializedValue;
            this._EvaluationResult = evalResult;
        }
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "AssemblyName", this.AssemblyName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "OutputTypeName", this.OutputTypeName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "OutputValue", this.SerializedOutputValue);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode,"EvaluationResult", this.EvaluationResult.ToString());

            return xNode;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="xNode"></param>
        /// <returns></returns>
        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                CodeRuleOutput output = new CodeRuleOutput();

                output.AssemblyName = XmlDataUtil.GetAttributeValue(xNode, "AssemblyName", "");
                output.OutputTypeName = XmlDataUtil.GetAttributeValue(xNode, "OutputTypeName", typeof(string).ToString());
                output.SerializedOutputValue = XmlDataUtil.GetAttributeValue(xNode, "OutputValue", "");
                output.EvaluationResult =
                    bool.Parse(XmlDataUtil.GetAttributeValue(xNode, "EvaluationResult", false.ToString()));

                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }
    }
}
